package com.zier.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zier.R;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    EditText et1, et2, et3, et4;

    public void onWechatPay(View view) {
        PayReq request = new PayReq();
        String nonceStr = et1.getText().toString().trim();
        String prepayId = et2.getText().toString().trim();
        String timeStamp = et3.getText().toString().trim();
        String sign = et4.getText().toString().trim();
        request.appId = "wx90e4b9de4dd9165f";
        request.packageValue = "Sign=WXPay";
        request.partnerId = "1433770902";
        if (TextUtils.isEmpty(nonceStr)) {
            request.nonceStr = "NG5MU0gmQKit4H2y";
        } else {
            request.nonceStr = nonceStr;
        }
        if (TextUtils.isEmpty(prepayId)) {
            request.prepayId = "wx2017021317011982119c13ba0927279010";
        } else {
            request.prepayId = prepayId;
        }
        if (TextUtils.isEmpty(timeStamp)) {
            request.timeStamp = "1486976479";
        } else {
            request.timeStamp = timeStamp;
        }
        if (TextUtils.isEmpty(sign)) {
            request.sign = "981C04BAEC05AF52B36ED8D31FB0A184";
        } else {
            request.sign = sign;
        }
        api.sendReq(request);
       // App.getInstance().wxapi.sendReq(request);
    }

    /* "appid": "wx4b85b85c2a9579e3",//应用ID
        "noncestr": "1qgxjEwRT6hXynmD",//随机字符串
        "package": "Sign=WXPay",//扩展字段
        "partnerid": "1433770902",//商户号
        "prepayid": "wx20170213171027cf2e0a1e2b0586419020",//预支付交易会话ID
        "timestamp": 1486977027,//时间戳
        "sign": "72002947075E8429F14D33D8EA647067"*/ //签名
    /*

            "appid": "wx4b85b85c2a9579e3",
            "noncestr": "NG5MU0gmQKit4H2y",
            "package": "Sign=WXPay",
            "partnerid": "1433770902",
            "prepayid": "wx2017021317011982119c13ba0927279010",
            "timestamp": 1486976479,
            "sign": "981C04BAEC05AF52B36ED8D31FB0A184"
            */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initView();

        api = WXAPIFactory.createWXAPI(WXPayEntryActivity.this, null);
        api.registerApp("wx4b85b85c2a9579e3");
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        et4 = (EditText) findViewById(R.id.et4);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                //goToGetMsg();
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                // goToShowMsg((ShowMessageFromWX.Req) req);
                break;
            default:
                break;
        }
    }


    @Override
    public void onResp(BaseResp resp) {

        int code = resp.errCode;

        switch (code) {
            case 0://支付成功后的界面
                Toast.makeText(this, "支付成功", Toast.LENGTH_LONG).show();
                break;
            case -1:
                Toast.makeText(this,"签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等。", Toast.LENGTH_LONG).show();
                break;
            case -2://用户取消支付后的界面
                Toast.makeText(this, "用户取消", Toast.LENGTH_LONG).show();

                break;
        }
        //微信支付后续操作，失败，成功，取消
      //  Toast.makeText(this, "errCode:" + resp.errCode + ",errStr:" + resp.errStr + ",openId:" + resp.openId, Toast.LENGTH_LONG).show();

    }


}